//
//  ThirdViewController.m
//  TakeHer
//
//  Created by Jupiter on 15/6/10.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ThirdViewController.h"
#import "MyCollectionReusableView.h"
#import "SelectionOneCollectionViewCell.h"
#import "AFNetworking.h"
//#import "TravelNotes.h"
#import "TravelNoteModel.h"
#import "UIImageView+WebCache.h"
#import "DetailInfo.h"
#import "ThirdViewDetailViewController.h"
#import "UIButton+WebCache.h"
#import "UserViewController.h"




@interface ThirdViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>


#define APP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
#define APP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger seconds;
@property (nonatomic, retain) NSString *titleName;
@property (nonatomic, retain) NSArray *titleArr;






@end

@implementation ThirdViewController

- (void)dealloc{
    [_arr release];
    [_imageArr release];
    [_titleArr release];
    [_titleName release];
    [_collectionView release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
        self.imageArr = [NSMutableArray array];
    }
    return self;
}



- (void)dataHandle
{
    //拼接网址
    NSString *str = @"http://api.breadtrip.com/trips/hot/?start=0&count=20&is_ipad=true";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //解析数据
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.imageArr = [responseObject objectForKey:@"trips"];
        
        
        
        
        [self.collectionView reloadData];
        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"OOOOOOOppppp");
    
    }];

     }


- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataHandle];
    // Do any additional setup after loading the view.
    
    [self dataHandle];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //每个cell的宽和高
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 20), (self.view.frame.size.width - 20) / 5 * 4);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; //竖直滚动
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    

    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //collectionView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_collectionView];
    
    //collectionView必须要注册一个cell类
    [_collectionView registerClass:[SelectionOneCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
/************************************************************/
    //这是设置section的header
    //collectionView给section注册一个view显示内容
//    [collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
/************************************************************/
    
    
    [_collectionView release];
    [flowLayout release];
    
    
    


}
#pragma 协议必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.imageArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //地点的网络请求label
    SelectionOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    //系统提供的cell不带任何控件,如果想使用cell显示信息,必须自定义
    cell.backgroundColor = [UIColor clearColor];
    cell.placeLabel.text = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"popular_place_str"];
    
    //标题的网络请求
    cell.nameLabel.text = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"name"];
    
    //天数的网络请求
    NSInteger aSeconds = 0;
    NSInteger bSeconds = 0;
    aSeconds = [[[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"last_day"] integerValue];
    bSeconds = [[[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"first_day"] integerValue];
    _seconds = aSeconds - bSeconds;
    cell.dateLabel.text = [TravelNoteModel day:_seconds];
    
    
    //设置头像按钮
    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    imageButton.frame = CGRectMake(cell.bounds.size.width - 40, cell.bounds.size.height - 45, 36, 36);
    imageButton.layer.cornerRadius = 18;
    imageButton.clipsToBounds = YES;
//    imageButton.backgroundColor = [UIColor yellowColor];
    [imageButton addTarget:self action:@selector(clickImageButton:) forControlEvents:UIControlEventTouchUpInside];
    [cell.myImageView setImageWithURLStr:[[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"cover_image_1600"]];
    [imageButton setImageWithURL:[NSURL URLWithString:[[[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"user"] objectForKey:@"avatar_l"]]];
    
    [cell addSubview:imageButton];
    
    
    
    return cell;
    
}

- (void)clickImageButton:(UIButton *)cb
{
    // NSLog(@"点击按钮 ===");
    UserViewController *userVC = [[UserViewController alloc]init];
    [self.navigationController pushViewController:userVC animated:YES];
    [userVC release];
    
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailInfo *info = [self.imageArr objectAtIndex:indexPath.row];
    ThirdViewDetailViewController *thirdDetailVC = [[ThirdViewDetailViewController alloc]init];
    thirdDetailVC.detail = info;
    thirdDetailVC.detailTitle = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"name"];
    thirdDetailVC.imageString = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"cover_image"];
    thirdDetailVC.userImageString = [[[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"user"] objectForKey:@"avatar_l"];
    thirdDetailVC.titleOfTop = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"name"];
   
    
    [self.navigationController pushViewController:thirdDetailVC animated:YES];
    
    thirdDetailVC.urlId = [[self.imageArr objectAtIndex:indexPath.item] objectForKey:@"id"];
    
    
    [thirdDetailVC release];
   
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.imageArr.count;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
